package com.github.controller.cms;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.common.bean.JsonResultBean;
import com.github.common.exception.BusinessException;
import com.github.common.util.validator.ValidGroupAdd;
import com.github.common.util.validator.ValidGroupUpdate;
import com.github.common.utils.spring.SpringBindingResultWrapper;
import com.github.form.cms.ResourceForm;
import com.github.module.cms.Resource;
import com.github.service.cms.ResourceService;

/**
 * <p>User: Mu Yunlong
 * <p>Date: 2016年2月23日 下午2:38:03
 * <p>Version: 1.0
 */
@Controller
@RequestMapping("/resource")
public class ResourceController {

    private static final String LIST_PAGE = "uum/resource/list";
    private static final String ADD_PAGE = "uum/resource/add";
    private static final String EDIT_PAGE = "uum/resource/edit";
    @Autowired
    private ResourceService resourceService;

    /**
     * 资源管理页面
     */
    @RequestMapping(value = "/list.gsp", method = RequestMethod.GET)
    public ModelAndView listPage() {
        ModelAndView mav = new ModelAndView(LIST_PAGE);
        List<Resource> resources = resourceService.findAll();
        mav.addObject("resources", resources);
        return mav;
    }

    /**
     * 新增页面
     */
    @RequestMapping(value = "/add_{parentId}.gsp", method = RequestMethod.GET)
    public final ModelAndView addPage(@PathVariable("parentId") final String parentId) {
        ModelAndView mav = new ModelAndView(ADD_PAGE);
        mav.addObject("parentId", parentId);
        return mav;
    }

    /**
     * 新增 Resource
     */
    @RequestMapping(value = "/add.gsp", method = RequestMethod.POST)
    @ResponseBody
    public final JsonResultBean add(@Validated({ValidGroupAdd.class }) @ModelAttribute("form") final ResourceForm form, final BindingResult bindingResult) {
        // 数据校验
        if (bindingResult.hasErrors()) {
            return new JsonResultBean(JsonResultBean.FAULT, SpringBindingResultWrapper.warpErrors(bindingResult));
        } else {
            resourceService.add(form);
            return new JsonResultBean(JsonResultBean.SUCCESS);
        }
    }

    /**
     * 修改页面
     */
    @RequestMapping(value = "/edit_{id}.gsp", method = RequestMethod.GET)
    public final ModelAndView editPage(@PathVariable final String id) {
        ModelAndView mav = new ModelAndView(EDIT_PAGE);
        mav.addObject("result", resourceService.get(id));
        return mav;
    }

    /**
     * 修改 Resource
     */
    @RequestMapping(value = "/edit.gsp", method = RequestMethod.POST)
    public @ResponseBody JsonResultBean edit(@Validated({ ValidGroupUpdate.class }) @ModelAttribute("form") final ResourceForm form,final BindingResult bindingResult) {
        // 数据校验
        if (bindingResult.hasErrors()) {
            return new JsonResultBean(JsonResultBean.FAULT, SpringBindingResultWrapper.warpErrors(bindingResult));
        } else {
            resourceService.update(form);
            return new JsonResultBean(JsonResultBean.SUCCESS);
        }
    }
    
    /**
     * 根据id删除 Resource
     */
    @RequestMapping(value = "/delete_{id}.gsp", method = RequestMethod.POST)
    public @ResponseBody JsonResultBean delete(@PathVariable("id") final String id)  {
        resourceService.delete(id);
        return new JsonResultBean(JsonResultBean.SUCCESS);
    }
    
    /**
     * 根据id启用 Resource
     * @throws BusinessException 
     */
    @RequestMapping(value = "/enable_{id}.gsp", method = RequestMethod.POST)
    @ResponseBody
    public final JsonResultBean enable(@PathVariable("id") final String id) throws BusinessException {
        resourceService.enable(id);
        return new JsonResultBean(JsonResultBean.SUCCESS);
    }

    /**
     * 根据id停用 Resource
     * @throws BusinessException 
     */
    @RequestMapping(value = "/disable_{id}.gsp", method = RequestMethod.POST)
    @ResponseBody
    public final JsonResultBean disable(@PathVariable("id") final String id) throws BusinessException  {
        resourceService.disable(id);
        return new JsonResultBean(JsonResultBean.SUCCESS);
    }
    
  

}
